package net.flydy.ywxcx.wxcloudrun.controller;


import lombok.extern.slf4j.Slf4j;
import net.flydy.ywxcx.wxcloudrun.common.*;
import net.flydy.ywxcx.wxcloudrun.model.*;
import net.flydy.ywxcx.wxcloudrun.dto.*;
import net.flydy.ywxcx.wxcloudrun.service.impl.*;
import net.flydy.ywxcx.wxcloudrun.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@Slf4j
@RequestMapping(value = "/merchant/")
public class MerchantController extends BaseController {
    private static final String LogPre = "/merchant";

    @Autowired
    private MerchantService merchantService;

    /**
     * 小程序login后，传入jscode过来，服务器访问小程序服务端获取openId, uniId等信息。服务端生成一个token
     * 多个商铺的话，返回商铺列表
     * 未绑定商铺，返回错误（下一步用户走商铺绑定逻辑）
     * @param httpReq
     * @param httpResp
     * @param request
     * @return
     */
    @RequestMapping(value = {"/merchantWXLogin"},
            produces = "application/json;charset=utf-8")
    @ResponseBody
    public BaseResponse doLogin(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody WXLoginReq request) {
        String interfaceType = LogPre + "/merchantWXLogin";
        BaseDTO<WXLoginReq, BaseResponse<WXLoginRespData>> dto = new BaseDTO<>();
        dto.setReqInterface(interfaceType);
        dto.setRequest(request);
        String reqIp = Utils.getIp(httpReq);
        dto.setReqIp(reqIp);
        return doService(()->{
            merchantService.wxLogin(dto);
            BaseResponse<WXLoginRespData> baseResponse = dto.getResponse();
            return baseResponse;
        }, dto);
    }

    //进入商铺， 检查登录状态，会话设置当前商铺id
//    public BaseResponse enterStore()

    /**
     * 商铺列表, 根据openId查已绑定列表
     * @param httpReq
     * @param httpResp
     * @return
     */
    @RequestMapping(value = {"/findMyStore"})
    @ResponseBody
    public BaseResponse findMyStore(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody FindMyStoreReq request) {
        MerchantDTO<FindMyStoreReq, BaseResponse<MyStoresResp>> dto = new MerchantDTO<>();
        baseSet(dto, httpReq, LogPre + "/findMyStore", request);
        return doService(()-> {
            merchantService.findMyStore(dto);
            return dto.getResponse();
        }, dto);
    }

    private void baseSet(MerchantDTO dto, HttpServletRequest httpReq, String interfaceType, BaseRequest request) {
        MyToken myToken = (MyToken) httpReq.getAttribute("token");
        dto.setMyToken(myToken);
        dto.setReqInterface(interfaceType);
        dto.setRequest(request);
        String reqIp = Utils.getIp(httpReq);
        dto.setReqIp(reqIp);
    }

    /**
     * 商铺列表， 根据 phoneNumber + bcode 查未绑定的商铺
     * @param httpReq
     * @param httpResp
     * @param request
     * @return
     */
    @RequestMapping(value = {"/findUnBindStore"})
    @ResponseBody
    public BaseResponse findUnBindStore(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody FindUnBindStoreReq request) {
        MerchantDTO<FindUnBindStoreReq, BaseResponse<MyStoresResp>> dto = new MerchantDTO<>();
        baseSet(dto, httpReq, LogPre + "/findUnBindStore", request);
        return doService(()-> {
            merchantService.findUnBindStore(dto);
            return dto.getResponse();
        }, dto);
    }

    //绑定商铺 wxId + phoneNumber 等 商铺id
    @RequestMapping(value = {"/bindStore"})
    @ResponseBody
    public BaseResponse bindStore(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody BindStoreReq request) {
        MerchantDTO<BindStoreReq, BaseResponse<BindStoreResp>> dto = new MerchantDTO<>();
        baseSet(dto, httpReq, LogPre + "/bindStore", request);
        return doService(()-> {
            merchantService.bindStore(dto);
            return dto.getResponse();
        }, dto);
    }

    //解除商铺绑定  wxId + 商铺id
    @RequestMapping(value = {"/unBindStore"})
    @ResponseBody
    public BaseResponse unBindStore(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody UnBindStoreReq request) {
        MerchantDTO<UnBindStoreReq, BaseResponse<UnBindStoreResp>> dto = new MerchantDTO<>();
        baseSet(dto, httpReq, LogPre + "/unBindStore", request);
        return doService(()-> {
            merchantService.unBindStore(dto);
            return dto.getResponse();
        }, dto);
    }

    //查看优惠券。 storeId, wxId 查看可用的优惠券. 前提store需要可用
    @RequestMapping(value = {"/findMyCoupons"})
    @ResponseBody
    public BaseResponse findMyCoupons(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody MerchantCouponReq request) {
        MerchantDTO<MerchantCouponReq, BaseResponse<MerchantCouponResp>> dto = new MerchantDTO<>();
        baseSet(dto, httpReq, LogPre + "/findMyCoupons", request);
        return doService(()-> {
            merchantService.findMyCoupons(dto);
            return dto.getResponse();
        }, dto);
    }

    //兑换优惠券。 优惠券id, wxId, 优惠券可用， 前提store需要可用? 是否需要兑换码
    @RequestMapping(value = {"/exchangeCoupons"})
    @ResponseBody
    public BaseResponse exchangeCoupons(HttpServletRequest httpReq, HttpServletResponse httpResp, @RequestBody MerchantCouponReq request) {
        MerchantDTO<MerchantCouponReq, BaseResponse<ExchangeCouponsResp>> dto = new MerchantDTO<>();
        baseSet(dto, httpReq, LogPre + "/exchangeCoupons", request);
        return doService(()-> {
            merchantService.exchangeCoupons(dto);
            return dto.getResponse();
        }, dto);
    }

}
